<?php
class Tourismorderexchange extends CS_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->library('pagination');
        $this->load->model('tourism_order_barter_model','tourism_order_barter');
        $this->load->model('tourism_order_model', 'tourism_order');
        $this->load->model('tourism_order_product_model', 'tourism_order_product');
        $this->load->model('express_record_model', 'express_record');
    }
    
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $getData['seller_uid'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismorderexchange/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url']    = base_url('tourismorderexchange/grid');
        $config['total_rows']  = $this->tourism_order_barter->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link']   = $this->pagination->create_links();
        $data['list'] = $this->tourism_order_barter->page_list($page_num, $num, $getData);
        $data['all_rows']  = $config['total_rows'];
        $data['pg_now']    = $pg;
        $data['pg_num'] = $page_num;
        $data['status'] = array('1' => '待审核', '2' => '同意', '3' => '拒绝');
        $data['state'] = array('1' => '未退款', '2' => '已退款');
        $this->load->view('tourismorderexchange/grid',$data);
    }
    
    public function examine($barter_id)
    {
        $params['barter_id'] = $barter_id;
        $result = $this->tourism_order_barter->examine($params);
        $examine = $result->row();

        if($examine->logistics){
            $data['logistics'] = $this->express_record->findByNu($examine->logistics);
        }
        $order = $this->tourism_order->findByOrderId($examine->order_id);
        $data['orderInfo'] = $order->row();
        $data['examine'] = $examine;
        $this->load->view('tourismorderexchange/examine', $data);
    }
    
    public function examinePost()
    {
        $barter_id = $this->input->post('barter_id');
        if ($this->validateParam($this->input->post('order_id')) || $this->validateParam($this->input->post('barter_num')) || $this->validateParam($this->input->post('goods_attr_id'))) {
            $this->error('tourismorderexchange/examine', $barter_id, '换货信息有误');
        }
        $this->db->trans_start();
        $result = $this->tourism_order_barter->updataExamineStatus($this->input->post());
        if ($this->input->post('status') == 3) {//拒绝退款需还原商品数量
            $this->tourism_order_product->setTourismNum($this->input->post());
        }
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismorderexchange/examine', $barter_id, '保存成功');
        } else {
            $this->error('tourismorderexchange/examine', $barter_id, '保存失败');
        }
    }
}